import 'dart:io';
import 'dart:ui' as ui;
import 'package:flutter/services.dart';
import 'package:path_provider/path_provider.dart';
import 'package:syncfusion_flutter_pdf/pdf.dart';
import 'package:qr_flutter/qr_flutter.dart';

class PdfService {
  Future<String> generateAcceptanceFormPdf({
    required String formNumber,
    required String date,
    required Map<String, dynamic> applicantInfo,
    required Map<String, dynamic> respondentInfo,
    required String disputeDescription,
    String? acceptorName,
    String? acceptorUnit,
    String? acceptorPhone,
  }) async {
    // 创建PDF文档
    final PdfDocument document = PdfDocument();
    final PdfPage page = document.pages.add();
    
    // 加载自定义字体
    final ByteData fontData = await rootBundle.load('assets/fonts/UKIJCJK.ttf');
    final PdfFont font = PdfTrueTypeFont(fontData.buffer.asUint8List(), 12);
    final PdfFont titleFont = PdfTrueTypeFont(fontData.buffer.asUint8List(), 18);
    
    // 创建PDF图形
    final PdfGraphics graphics = page.graphics;
    
    // 生成二维码
    final qrImage = await QrPainter(
      data: formNumber,
      version: QrVersions.auto,
      gapless: true,
      color: const Color(0xFF000000),
      emptyColor: const Color(0xFFFFFFFF),
    ).toImageData(100.0); // 将二维码尺寸从120缩小到100
    
    if (qrImage != null) {
      // 将QR码转换为PDF图形
      final PdfBitmap qrBitmap = PdfBitmap(qrImage.buffer.asUint8List());
      graphics.drawImage(
        qrBitmap,
        Rect.fromLTWH(420, 20, 50, 50), // 调整位置和大小：更靠左（420），显示尺寸更小（50x50）
      );
    }
    
    // 添加标题
    graphics.drawString(
      '洛浦县矛盾纠纷受理告知单',
      titleFont,
      bounds: Rect.fromLTWH(180, 20, 300, 50),
    );
    
    // 添加基本信息
    graphics.drawString('编号：$formNumber', font,
        bounds: Rect.fromLTWH(50, 70, 200, 20));
    graphics.drawString('日期：$date', font,
        bounds: Rect.fromLTWH(50, 90, 200, 20));
        
    // 添加申请人信息
    graphics.drawString('当事人信息', font,
        bounds: Rect.fromLTWH(50, 120, 200, 20));
    graphics.drawString('1. 申请人（甲方）：', font,
        bounds: Rect.fromLTWH(50, 140, 200, 20));
    graphics.drawString('姓名：${applicantInfo['name']}    联系方式：${applicantInfo['contact']}', font,
        bounds: Rect.fromLTWH(70, 160, 400, 20));
    graphics.drawString('身份证号/单位名称：${applicantInfo['idNumber']}', font,
        bounds: Rect.fromLTWH(70, 180, 400, 20));
        
    // 添加被申请人信息
    graphics.drawString('2. 被申请人（乙方）：', font,
        bounds: Rect.fromLTWH(50, 200, 200, 20));
    graphics.drawString('姓名：${respondentInfo['name']}    联系方式：${respondentInfo['contact']}', font,
        bounds: Rect.fromLTWH(70, 220, 400, 20));
    graphics.drawString('身份证号/单位名称：${respondentInfo['idNumber']}', font,
        bounds: Rect.fromLTWH(70, 240, 400, 20));
        
    // 添加纠纷描述
    graphics.drawString('纠纷简要描述：', font,
        bounds: Rect.fromLTWH(50, 270, 200, 20));
    graphics.drawString(disputeDescription, font,
        bounds: Rect.fromLTWH(70, 290, 400, 80));
        
    // 添加权利义务告知
    graphics.drawString('经审查，您提交的纠纷申请材料符合受理条件，现予以正式受理。', font,
        bounds: Rect.fromLTWH(50, 380, 500, 20));
    graphics.drawString('权利义务告知：', font,
        bounds: Rect.fromLTWH(50, 400, 200, 20));
    graphics.drawString('1. 申请人应保证提交材料的真实性，否则承担相应法律责任。', font,
        bounds: Rect.fromLTWH(70, 420, 500, 20));
    graphics.drawString('2. 双方有权申请调解员回避或提出处理意见。', font,
        bounds: Rect.fromLTWH(70, 440, 500, 20));
    graphics.drawString('3. 调解过程中应遵守秩序，尊重调解人员及其他当事人。', font,
        bounds: Rect.fromLTWH(70, 460, 500, 20));

    // 添加备注
    graphics.drawString('备注：', font,
        bounds: Rect.fromLTWH(50, 490, 200, 20));
    graphics.drawString('1. 本告知单一式两份，当事人与受理机构各执一份。', font,
        bounds: Rect.fromLTWH(70, 510, 500, 20));
    graphics.drawString('2. 如需进度查询，请拨打上述联系电话并提供受理编号。', font,
        bounds: Rect.fromLTWH(70, 530, 500, 20));
    graphics.drawString('3. 咨询办理进度，请联系：', font,
        bounds: Rect.fromLTWH(70, 550, 500, 20));
    graphics.drawString('受理人：${acceptorName ?? "_________"}    联系电话：${acceptorPhone ?? "_________"}', font,
        bounds: Rect.fromLTWH(90, 570, 500, 20));
    graphics.drawString('受理人单位：${acceptorUnit ?? "_________"}', font,
        bounds: Rect.fromLTWH(90, 590, 500, 20));
        
    // 添加签收确认
    graphics.drawString('签收确认：', font,
        bounds: Rect.fromLTWH(50, 620, 200, 20));
    graphics.drawString('申请人（签字/盖章）：_____________ 日期：_____________', font,
        bounds: Rect.fromLTWH(70, 640, 500, 20));
    graphics.drawString('受理机构（盖章）：_____________ 日期：_____________', font,
        bounds: Rect.fromLTWH(70, 660, 500, 20));
    
    // 保存文件
    final Directory tempDir = await getTemporaryDirectory();
    final String filePath = '${tempDir.path}/acceptance_form_$formNumber.pdf';
    final File file = File(filePath);
    await file.writeAsBytes(await document.save());
    document.dispose();
    
    return filePath;
  }
} 